package service.impl;

import execption.nullValueException;
import execption.repeatValueException;
import service.FlowerStoreService;
import dao.*;
import dao.impl.*;
import domain.*;
import utill.ParamUtil;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FlowerStoreServiceImpl implements FlowerStoreService {
    flowerstoreMapper flowerstoreMapper =new flowerstoreMapperImpl();
    flowerMapper  flowerMapper =new flowerMapperImpl();
    @Override
    public boolean flowerStoreLogin(String storeName, String storePassword) {
        flowerstore flowerstore = flowerstoreMapper.findByUsernameAndPassword(storeName,storePassword);
        if(flowerstore!=null) {
            ParamUtil.storeId.put(Thread.currentThread().getName() + "storeId", flowerstore.getStoreId());
        }
        return flowerstore==null ? false :true;
    }

    @Override
    public List<OrderResult> orderList() {
        return flowerstoreMapper.findOrderResult();
    }

    @Override
    public boolean InputFlower(int id, int total) throws nullValueException, SQLException {
        flower f=new flower();
        f.setFlowerId(id);
        f.setFlowertotal(total);
        int n=flowerMapper.updateFlower(f);

        return  n==0 ? false :true;

    }

    @Override
    public boolean InputFlower(int id, int total, boolean flag) throws nullValueException, SQLException {
        flower f=new flower();
        f.setFlowerId(id);
        f.setFlowertotal(total);
        return  flowerMapper.updateFlower(f,true)==0 ? false :true;
    }

    @Override
    public boolean AddFlowers(flower f) throws repeatValueException {
        return flowerMapper.addFlower(f)==0 ? false :true;
    }

    /**
     *  从内存中读取数据并返回;
     */

    @Override
    public Set<OrderResult> filterOrder(String type , String price , String time ,List<OrderResult> list){

        Set<OrderResult> set =new HashSet<>();
        for(OrderResult or :list){
            boolean flag=false;
            if(!"".equals(price) && or.getFlowerPrice().equals(price)){
                flag=true;
            }

            if(!"".equals(type) && or.getFlowerType().equals(type)){
                flag=true;
            }

            if(!"".equals(time) && or.getFlowerBuyTime().indexOf(time)!=-1){
                flag=true;
            }
            if(flag) set.add(or);
        }
        return  set;
    }
}
